using Avalonia.Controls;
using Avalonia.Interactivity;
using IntegratedMeasuring.ViewModels.Scheme;
using IntegratedMeasuring.Storage;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Avalonia.Collections;

namespace IntegratedMeasuring.Pages;

public partial class TestRecordDetailDialog : Window
{
    private ProductInfo _productInfo;
    private List<TestRecord> _testRecords;

    public TestRecordDetailDialog(ProductInfo productInfo)
    {
        InitializeComponent();
        _productInfo = productInfo;
        Loaded += OnLoaded;
    }

    private async void OnLoaded(object sender, RoutedEventArgs e)
    {
        await LoadDetailData();
    }

    private async Task LoadDetailData()
    {
        try
        {
            // 显示基本信息
            this.FindControl<TextBlock>("DetailBarcodeTextBlock").Text = _productInfo.BarCode;
            this.FindControl<TextBlock>("DetailOrderNameTextBlock").Text = _productInfo.OrderName;
            this.FindControl<TextBlock>("DetailTestConditionTextBlock").Text = _productInfo.TestCondition;
            this.FindControl<TextBlock>("DetailStartTimeTextBlock").Text = _productInfo.StartTime.ToString("yyyy-MM-dd HH:mm:ss");
            this.FindControl<TextBlock>("DetailEndTimeTextBlock").Text = _productInfo.EndTime.ToString("yyyy-MM-dd HH:mm:ss");

            // 设置测试结果显示
            var resultTextBlock = this.FindControl<TextBlock>("DetailResultTextBlock");
            resultTextBlock.Text = _productInfo.Result switch
            {
                1 => "PASS",
                0 => "FAIL",
                2 => "NULL",
                _ => "UNKNOWN"
            };

            // 设置结果背景色
            var resultBorder = this.FindControl<Border>("DetailResultBorder");
            var resultColor = _productInfo.Result switch
            {
                1 => "#D4EDDA", // 浅绿色
                0 => "#F8D7DA", // 浅红色
                2 => "#E2E3E5", // 浅灰色
                _ => "#6C757D"  // 灰色
            };
            resultBorder.Background = new Avalonia.Media.SolidColorBrush(Avalonia.Media.Color.Parse(resultColor));

            // 加载测试明细数据
            _testRecords = await BaseDataStorage.Instance.QueryAsync<TestRecord>(x => x.ProductId == _productInfo.Id);
            TestConditions? testConditions = await BaseDataStorage.Instance.QueryTestConditionsByNameAsync(_productInfo.TestCondition);
            // 转换为TransItemData格式以显示更多信息
            var detailData = new List<TransItemData>();
            foreach (var record in _testRecords)
            {
                var transItem = new TransItemData
                {
                    Id = record.Id,
                    ProductId = record.ProductId,
                    Pri = record.Pri,
                    Sec = record.Sec,
                    MinorTerm = record.MinorTerm,
                    Unit = record.Unit,
                    DetectionValue = record.DetectionValue,
                    DisplayDetectionValue = record.DisplayDetectionValue,
                    DisplayUnit = record.DisplayUnit,
                    Result = record.Result
                };
                detailData.Add(transItem);
            }

            if (testConditions != null)
            {
                foreach (var record in detailData)
                {
                    TransItemData? beautifyData = testConditions.BeautifyData.Find(x =>
                        x.MinorTerm == record.MinorTerm && x.Pri == record.Pri && x.Sec == record.Sec);
                    if (beautifyData != null)
                    {
                        record.Pins = beautifyData.Pins;
                        record.ParallelPin = beautifyData.ParallelPin;
                        record.SeriesPin = beautifyData.SeriesPin;
                        record.ShortCircuitPin = beautifyData.ShortCircuitPin;
                        record.Nominal = beautifyData.Nominal;
                        record.LowerLimit = beautifyData.LowerLimit;
                        record.UpperLimit = beautifyData.UpperLimit;
                        record.Freq = beautifyData.Freq;
                        record.Vol = beautifyData.Vol;
                    }
                }
            }
            // 绑定明细数据到DataGrid
            var detailDataGrid = this.FindControl<DataGrid>("DetailDataGrid");
            var detailView = new DataGridCollectionView(detailData);
            detailDataGrid.ItemsSource = detailView;
        }
        catch (Exception ex)
        {
            // 可以添加错误处理
            System.Diagnostics.Debug.WriteLine($"加载详情数据失败: {ex.Message}");
        }
    }

    private void CloseButton_Click(object? sender, RoutedEventArgs e)
    {
        Close();
    }
} 